<?php
namespace app\index\controller;
use think\Db;
use app\index\controller\Base;
use PHPExcel_IOFactory;
use PHPExcel;
class Course extends Base
{
    public function addCourse()
    {
        $data = input('post.');
        $res = Db::name('course')->insert($data);
        if ($res){
            $this->success('添加成功','/index/User/addCourse');
        }else{
            $this->error('添加失败');
        }
    }
    public function addCourseUser()
    {
        $session= session('user_id');
        $data =input('post.');
        $res = Db::name('courseuser')->data(['course_id'=>$data['courseId'],'user_id'=>$session])->insert();
        if ($res){
            $this->success('添加成功');
        }
    }
    public function addCourseProject()
    {
        $data=input('post.');
        if ($data['projectStatus']=="分组"){
            $data['projectStatus']=1;
        }else{
            $data['projectStatus']=0;
        }

        $project=['name'=>$data['projectName'],'describe'=>$data['projectDescribe'],'status'=>$data['projectStatus']];
        $res = Db::name('project')->insertGetId($project);
        $result = Db::name('courseproject')->data(['course_id'=>$data['courseId'],'project_id'=>$res])->insert();
        if($result){
            $this->success('实验添加成功');
        }
        else{
            $this->error('实验添加失败');
        }
    }
    public function courseList()
    {
        $data = input('get.');
        $this->assign('data',$data);

        $result = Db::name('courseproject')->alias('a')->join('project b','a.project_id=b.project_id','RIGHT')->where('a.course_id',$data['course_id'])->select();
        $this->assign('result',$result);

        $res = Db::name('class')->select();
        $this->assign('res',$res);


        return $this->fetch('user/courseList');
    }

    /**
     * 写蒙了，这个还不知道干啥用
     */
//    public function addCourseProjectClass()
//    {
//        exit;
//        $class_id = input('post.');
//        $course_id = input('get.');
//        $result = Db::name('courseclass')->data(['course_id'=>$course_id['course_id'],'class_id'=>$class_id['classId']])->insert();
//        if ($result){
//            $this->success('添加成功');
//        }else{
//            $this->error('添加失败');
//        }
//    }

    public function addCoursePerson()
    {
        $data = input('post.');
        $find = Db::name('courseclass')->where(['course_id'=>$data['course_id'],'class_id'=>$data['classId']])->select();
        if ($find){
            $this->error('该班级已经存在了');
            exit;
        }
        $result = Db::name('courseclass')->data(['course_id'=>$data['course_id'],'class_id'=>$data['classId']])->insert();
        if ($result){
            $this->success('添加成功');
        }else{
            $this->error('添加失败');
        }
    }
    public function courseLook()
    {
        $data = input('get.');
        $class = Db::name('courseclass')->alias('a')->join('class b','a.class_id=b.class_id','RIGHT')->where('a.course_id',$data['course_id'])->select();
        $this->assign('class',$class);
        $this->assign('project_id',$data['project_id']);
        $this->assign('data',$data);
        return $this->fetch('user/courseLook');
    }
    public function classLook()
    {
        @$data = input('get.');
        $class = Db::name('courseclass')->alias('a')->join('class b','a.class_id=b.class_id','RIGHT')->where('a.course_id',$data['course_id'])->select();
        $this->assign('class',$class);//所属这个课程的班级
        if ($data['status']==0){
            $person = Db::name('classperson')->alias('a')->join('person b','a.person_id=b.person_id','RIGHT')->where('a.class_id',$data['class_id'])->select();
            $this->assign('person',$person);//所属这个课程的person
        }else{
            $person = Db::name('classperson')->alias('a')->join('person b','a.person_id=b.person_id','RIGHT')->where(['a.class_id'=>$data['class_id'],'b.status'=>$data['status']])->select();
            $this->assign('person',$person);//所属这个课程的person
        }
        $file = Db::name('file')->alias('a')->join('person b','a.belong_who_id=b.person_id ','INNER')->where('belong_project_id',$data['project_id'])->select();
        $this->assign('file',$file);
        $this->assign('project_id',$data['project_id']);
        $this->assign('course_id',$data['course_id']);
        $this->assign('data',$data);
        $group = array();
        $i = 0;
        foreach ($person as $row){
            $res = Db::name('grade')->where(['person_id'=>$row['person_id'],'project_id'=>$data['project_id']])->select();
            $result = Db::name('gradeuser')->where(['person_id'=>$row['person_id'],'project_id'=>$data['project_id'],'course_id'=>$data['course_id']])->find();
            $gr = array(); $j =0;
            foreach ($res as $re){
                $re = Db::name('person')->where('person_id',$re['make_person_id'])->find();
                $gr[$j] = $re['name'];
                $j++;
            }
            $all = round(((@$res[0]['grade']+@$res[1]['grade']+@$res[2]['grade'])/3)*0.5+@$result['grade']*0.5,2);
//            if ($gr==null || $res[0]['grade'] == 0) {
//                $group[$i]=['name'=>$row['name'],'student1'=>"未接受评分",'student2'=>"未接受评分",'student3'=>"未接受评分",'teacher'=>@$result['grade'],'all'=>$all];
//            }else{
//                if(){
//                    $group[$i]=['name'=>$row['name'],'student1'=>"未评分",'student2'=>"未评分",'student3'=>"未评分",'teacher'=>@$result['grade'],'all'=>$all];
//                } else
            $student3 = (@$res[2]['grade'] == 0 || @$res[2]['grade'] == null) ? "未接受评分" : "账号".@$gr[2]."打分".@$res[2]['grade'] ;
            $student2 = (@$res[1]['grade'] == 0 || @$res[1]['grade'] == null) ? "未接受评分" : "账号".@$gr[1]."打分".@$res[1]['grade'] ;
            $student1 = (@$res[0]['grade'] == 0 || @$res[0]['grade'] == null) ? "未接受评分" : "账号".@$gr[0]."打分".@$res[0]['grade'] ;
//                if (@$res[2]['grade'] == 0 || @$res[2]['grade'] == null){
//                    $student3 = "未接受评分";
//                } else  $student3 = "账号".@$gr[2]."打分".@$res[2]['grade'];
//                if (@$res[1]['grade'] == 0 || @$res[1]['grade'] == null) {
//                    $student2 = "未接受评分";
//                } else $student2 = "账号".@$gr[1]."打分".@$res[1]['grade'];
//                if (@$res[0]['grade'] == 0 || @$res[0]['grade'] == null) {
//                    $student1 = "未接受评分";
//                } else $student1 = "账号".@$gr[0]."打分".@$res[0]['grade'];

                $group[$i]=['name'=>$row['name'],'student1'=>$student1,'student2'=>$student2,'student3'=>$student3,'teacher'=>@$result['grade'],'all'=>$all];
                //
          //  }
            $i++;
        }
        $this->assign('group',$group);
//        if(){
//            require_once APP_PATH . '/../vendor/PHPExcel-1.8/Classes/PHPExcel.php';
//            // dump($value);
//            $file_name = "打分表" . '.xls';
//            $path = dirname(__FILE__);
//            $PHPExcel = new \PHPExcel();
//            // print_r($PHPExcel);die;
//
//            $PHPSheet = $PHPExcel->getActiveSheet();
//            $PHPSheet->getColumnDimension( 'A')->setWidth(300);
//            $PHPSheet->setTitle("打分表");
//            $PHPSheet->setCellValue("A1", "学生");
//            $PHPSheet->setCellValue("B1", "打分学生1");
//            $PHPSheet->setCellValue("C1", "打分学生2");
//            $PHPSheet->setCellValue("D1", "打分学生3");
//            $PHPSheet->setCellValue("E1", "教师评分");
//            $PHPSheet->setCellValue("F1", "总分");
//            $i=2;
//            foreach ($group as $row):
//                $PHPSheet->setCellValue('A'.$i,''.$row['name']);
//                $PHPSheet->setCellValue('B'.$i,''.$row['student1']);
//                $PHPSheet->setCellValue('C'.$i,''.$row['student2']);
//                $PHPSheet->setCellValue('D'.$i,''.$row['student3']);
//                $PHPSheet->setCellValue('E'.$i,''.$row['teacher']);
//                $PHPSheet->setCellValue('F'.$i,''.$row['all']);
//                ++$i;
//                //var_dump($group);
//            endforeach;
//
//            $fileName = iconv("utf-8", "utf-8", $file_name); // 重命名表
//            header('Content-Type: application/vnd.ms-excel');
//
//            header("Content-Disposition: attachment;filename=$fileName");
//
//            header('Cache-Control: max-age=0');
//
//            $objWriter = \PHPExcel_IOFactory::createWriter($PHPExcel, 'Excel5');
//
//            $objWriter->save('php://output'); // 文件通过浏览器下载
//        }

        return $this->fetch('user/classLook');
    }
    public function userMakeGrade()
    {
        $data = input('post.');
        $res = Db::name('gradeuser')->data(['person_id'=>$data['person_id'],'project_id'=>$data['project_id'],'course_id'=>$data['course_id'],'make_user_id'=>session('user_id'),'grade'=>$data['grade']])->insert();
        if ($res){
            $this->success('提交成功');
        }else{
            $this->error('失败');
        }
    }
    public function excel()
    {
        require_once APP_PATH . '/../vendor/PHPExcel-1.8/Classes/PHPExcel.php';
        // dump($value);
        $file_name = "打分表" . '.xls';
        $path = dirname(__FILE__);
        //Loader::import('PHPExcel.Classes.PHPExcel');
        //Loader::import('PHPExcel.Classes.PHPExcel.IOFactory.PHPExcel_IOFactory');
        $group = array();
        $i = 0;
        $person = Db::name('classperson')->alias('a')->join('person b','a.person_id=b.person_id','RIGHT')->where('a.class_id',session('this_class_id'))->select();

        foreach ($person as $row){
            $res = Db::name('grade')->where(['person_id'=>$row['person_id'],'project_id'=>session('this_project_id')])->select();
            $result = Db::name('gradeuser')->where(['person_id'=>$row['person_id'],'project_id'=>session('this_project_id'),'course_id'=>session('this_course_id')])->find();
            $gr = array(); $j =0;
            foreach ($res as $re){
                $re = Db::name('person')->where('person_id',$re['make_person_id'])->find();
                $gr[$j] = $re['name'];
                $j++;
            }
            $all = round(((@$res[0]['grade']+@$res[1]['grade']+@$res[2]['grade'])/3)*0.5+@$result['grade']*0.5,2);
            $group[$i]=['name'=>$row['name'],'student1'=>"账号".@$gr[0]."打分".@$res[0]['grade'],'student2'=>"账号".@$gr[1]."打分".@$res[1]['grade'],'student3'=>"账号".@$gr[2]."打分".@$res[2]['grade'],'teacher'=>@$result['grade'],'all'=>$all];
            $i++;
        }
        //dump($group);
        $PHPExcel = new \PHPExcel();
        // print_r($PHPExcel);die;
        $PHPSheet = $PHPExcel->getActiveSheet();
        $PHPSheet->getColumnDimension( 'A')->setWidth(30);
        $PHPSheet->getColumnDimension( 'B')->setWidth(30);
        $PHPSheet->getColumnDimension( 'C')->setWidth(30);
        $PHPSheet->getColumnDimension( 'D')->setWidth(30);
        $PHPSheet->getColumnDimension( 'E')->setWidth(30);
        $PHPSheet->getColumnDimension( 'F')->setWidth(30);
        $PHPSheet->setTitle("打分表");
        $PHPSheet->setCellValue("A1", "学生");
        $PHPSheet->setCellValue("B1", "打分学生1");
        $PHPSheet->setCellValue("C1", "打分学生2");
        $PHPSheet->setCellValue("D1", "打分学生3");
        $PHPSheet->setCellValue("E1", "教师评分");
        $PHPSheet->setCellValue("F1", "总分");
$i=2;
foreach ($group as $row){
    $PHPSheet->setCellValue('A'.$i,''.$row['name']);
    $PHPSheet->setCellValue('B'.$i,''.$row['student1']);
    $PHPSheet->setCellValue('C'.$i,''.$row['student2']);
    $PHPSheet->setCellValue('D'.$i,''.$row['student3']);
    $PHPSheet->setCellValue('E'.$i,''.$row['teacher']);
    $PHPSheet->setCellValue('F'.$i,''.$row['all']);
    ++$i;
    //var_dump($group);
}

        $fileName = iconv("utf-8", "utf-8", $file_name); // 重命名表
        header('Content-Type: application/vnd.ms-excel');

        header("Content-Disposition: attachment;filename=$fileName");

        header('Cache-Control: max-age=0');

        $objWriter = \PHPExcel_IOFactory::createWriter($PHPExcel, 'Excel5');

        $objWriter->save('php://output'); // 文件通过浏览器下载
    }
}